package chai.ningning.myself;

/**
 * 两个线程分别输出100以内的奇数、偶数
 */
public class Page142 {

    static class Odd implements Runnable{

        @Override
        public synchronized void run() {
            System.out.println(Thread.currentThread().getName());
            for (int i = 1; i < 100; i++) {
                if ( i%2 == 0){
                    System.out.print(i+" ");
                }
            }
            System.out.println(Thread.currentThread().getName());
            for (int i = 1; i < 100; i++) {
                if ( i%2 != 0){
                    System.out.print(i+" ");
                }
            }
        }
    }

    public static void main(String[] args) {
        Runnable r = new Odd();
        Thread first = new Thread(r);
        Thread second = new Thread(r);

        first.setName("first");
        second.setName("second");
        first.start();
        second.start();

    }
}
